import 'package:flutter/material.dart';
import 'package:wallpaper/components/my_tab_scroll_header.dart';
import 'package:wallpaper/pages/other/moe/moe_list.dart';

class MoePage extends StatefulWidget {
  const MoePage({super.key});

  @override
  State<MoePage> createState() => _MoePageState();
}

class _MoePageState extends State<MoePage> with TickerProviderStateMixin {
  late TabController tabController;
  int activeIndex = 0;
  PageController pageController = PageController(initialPage: 0);
  List types = [
    ['random', '全部'],
    ['approve', '过审'],
    ['top', '精选'],
    ['cat', '猫耳'],
    ['yin', '银发'],
    ['mp', '竖屏'],
    ['pc', '横屏'],
    ['tuzi', '点兔']
  ];
  List<String> subList = [];

  @override
  void initState() {
    super.initState(); 
    subList = types.map((e) => e[1].toString()).toList();
    tabController = TabController(length: subList.length, vsync: this);
    tabController.addListener(() {
      setState(() {
        activeIndex = tabController.index;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          toolbarHeight: 40,
          backgroundColor: Theme.of(context).colorScheme.primaryContainer,
          surfaceTintColor: Theme.of(context).colorScheme.primaryContainer,
          elevation: 0,
          title: MyTabScrollHeader(
              tabController: tabController,
              tabs: subList,
              onTap: (index) {
                pageController.jumpToPage(index);
              })),
      body: Column(
        children: [
          Expanded(
              child: PageView(
            controller: pageController,
            children: [
              for (var i = 0; i < subList.length; i++)
                MoeList(sort: types[i][0]),
            ],
            onPageChanged: (index) {
              setState(() {
                activeIndex = index;
              });
              tabController.animateTo(index);
            },
          ))
        ],
      ),
    );
  }
}
